package com.shinhye.mobilelottery.ui;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.shinhye.mobilelottery.ConstantValue;
import com.shinhye.mobilelottery.R;
import com.shinhye.mobilelottery.ui.adapter.PoolAdapter;
import com.shinhye.mobilelottery.ui.manager.TitleManager;

public class PlaySSQUI1 extends BaseUI {
	private GridView redGrid;
	private GridView blueGrid;
	private ArrayList<Integer> redNum;
	private ArrayList<Integer> blueNum;
	private PoolAdapter redAdapter;
	private PoolAdapter blueAdapter;

	public PlaySSQUI1(Context context) {
		super(context);
	}
	// 通用三步
	// 三个容器联动

	// 工作清单
	// ①标题处理
	// ②填充选号容器（红球与蓝球）
	// ③处理选号（单选+机选）
	
	// 单选处理
	// 左右摇晃
	// 放大效果的实现
	
	
	
	// ④手机摇晃处理
	// ⑤底部导航处理（提示信息+清空+选好了）
	@Override
	protected void onCreate() {
		View showView = View.inflate(getContext(), R.layout.il_playssq1, null);
		redGrid = (GridView) showView.findViewById(R.id.ii_ssq_red_number_container);
		blueGrid = (GridView) showView.findViewById(R.id.ii_ssq_blue_number_container);
		
		redAdapter = new PoolAdapter(getContext(), 33, redNum, R.drawable.id_redball);
		blueAdapter = new PoolAdapter(getContext(), 16, blueNum, R.drawable.id_blueball);
		
		redNum = new ArrayList<Integer>();
		blueNum = new ArrayList<Integer>();
		
		redGrid.setAdapter(redAdapter);
		blueGrid.setAdapter(blueAdapter);
		
		setContentView(showView);
	}

	@Override
	protected void setListener() {
		blueGrid.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 判断该位置上的号码是否被选中了
				// 如果没有被选中
				// 修改背景图片
				// 处理左右摇晃的动画
				// 添加号码到集合中
				// 被选中了
				// 还原背景图片
				// 删除号码
				
				if(!blueNum.contains(position)) {
					view.setBackgroundResource(R.drawable.id_blueball);
					Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.ia_ball_shake);
					view.startAnimation(anim);
					blueNum.add(position);
				} else {
					view.setBackgroundResource(R.drawable.id_defalut_ball);
					blueNum.remove(position);
				}
			}
		});
	}

	@Override
	public int getId() {
		return ConstantValue.VIEW_PLAYSSQ;
	}
	
	@Override
	public void onResume() {
		changeTitle();
		clear();
	}

	/**
	 * Clear the select state of this UI
	 */
	public void clear() {
		redNum.clear();
		blueNum.clear();
		
		redAdapter.notifyDataSetChanged();
		blueAdapter.notifyDataSetChanged();
	}
	
	private void changeTitle() {
		Bundle bundle = getBundle();
		String issue = "";
		String title = "";
		if(bundle != null) {
			issue = bundle.getString("issue");
		}
		if(TextUtils.isEmpty(issue)) {
			title = "双色球选号";
		} else {
			title = "双色球第" + bundle.getString("issue") + "期选号";
		}
		TitleManager.getInstance().changeTitle(title);
	}

}
